package
{	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.ProgressEvent;
	
	import newx.NxApi;
	import newx.com.ResManager;
	import newx.com.config.TextIni;
	import newx.com.loader.SwfLoader;
	import newx.utils.debug.DebugWindow;
	
	
	public class GameEngine extends Sprite
	{
		/**引擎接口载体（实例唯一）*/
		static private		var _nxApi			:NxApi;
				
		/**主配置文件*/
		private 			var _mainIni		:TextIni;
		
		private		const	_initTotal		:int	=	4;
		
		
		
		/**获取引擎接口*/
		static public function get _gameApi():NxApi
		{
			if(_nxApi == null)
			{
				_nxApi	=	new NxApi;
			}
			return _nxApi
		}
		
		/**
		 * 派发初始化事件
		 * @param val ：当前步骤。
		 * @param pro ：当前步骤进度。
		 * 
		 */
		protected function DispatchInitProgressEvent(step:int,stepTotal:int,pro:Number = 1):void
		{
		}
		
		//======================================================================
		
		public function GameEngine()
		{
			addEventListener("InitEngine",InitEngineHandle);
		}
		
		/**
		 * 引擎初始化
		 * */
		private function InitEngineHandle(e:*):void
		{
			var pList:Array	=	e.data;
			DispatchInitProgressEvent(1,_initTotal,1);
			
			//初始化加载器
			if(pList[0] is Function)
			{
				SwfLoader.isTest	=	true;
				SwfLoader.setUnEncryptByte(pList[0]);
			}
			
			//初始化数据模型
			_mainIni	=	new TextIni();
			_mainIni.setInitStr(pList[1]);
			
			//初始化api
			_gameApi.initEngineInfo(loaderInfo,pList[2],pList[3],pList[4]);
			
			_gameApi.clearStage();
			
			_gameApi.setStageBG(
				_mainIni.getContent("MainConfig","layoutbgres"),
				_mainIni.getContent("MainConfig","layouttype"));
			
//			_gameApi.httpValObj.debug =	"true";
			
			if(_gameApi.httpValObj.debug == "true")
			{
				DebugWindow.InitDebug(_gameApi.stage);
				DebugWindow.enabled		=	true
				DebugWindow.outMessage	=	true;
			}
			
			//启动主app逻辑模块
			Init();
			
			LoadGameConfig();
		}
		
		/**
		 * 完成基本初始化后调用，此时引擎任然在加载资源等其他项目。
		 * */
		protected function Init():void
		{
			
		}
		
		/**
		 * 加载游戏配置包
		 * */
		private function LoadGameConfig():void
		{
			DispatchInitProgressEvent(2,_initTotal,1);
			
			//加载配置
			_gameApi.Loader_StartLoad(
				_mainIni.getContent("MainConfig","configres"),
				null,
				startLoadControlRes,
				[configLoadingHandle]);
		}
		
		private function configLoadingHandle(e:*):void
		{
			if(e is ProgressEvent)
			{
				DispatchInitProgressEvent(3,_initTotal,e.bytesLoaded/e.bytesTotal);
			}
		}
		
		/**
		 * 配置文件加载完成，载入控件皮肤资源 
		 * @param val
		 * @param pList
		 * 
		 */
		private function startLoadControlRes(val:DisplayObject,pList:Array):void
		{
			if(val == null)
			{
				_gameApi.IO_TraceError("Load GameConfig Error.");
				return;
			}
			_gameApi.Config_Init(val);
			
			_gameApi.Loader_StartLoad(
				_mainIni.getContent("MainConfig","controlres"),
				null,
				onLoginResLoaded,
				[uiResLoadingHandle]);
		}
		
		private function uiResLoadingHandle(e:*):void
		{
			if(e is ProgressEvent)
			{
				DispatchInitProgressEvent(4,_initTotal,e.bytesLoaded/e.bytesTotal);
			}
		}
		
		/**
		 * 资源加载完成，开始加载模块文件
		 * 
		 */		
		private function onLoginResLoaded(val:DisplayObject,pList:Array):void
		{	
			ResManager.SaveUISkin(val);
			_gameApi.EngineInited	=	true;
//			trace("引擎初始化完成！");
			AppStart();
		}
		
		/**
		 * 完成所有初始化
		 * */
		protected function AppStart():void
		{
			
		}
	}
}